package com.hr.framework.web.service;

import com.hr.common.core.domain.entity.SysDept;
import com.hr.common.core.domain.entity.SysUser;
import com.hr.common.utils.DateUtils;
import com.hr.common.utils.ShiroUtils;
import com.hr.common.utils.security.PermissionUtils;
import com.hr.system.domain.SysPersonnelClock;
import com.hr.system.domain.WorkExperience;
import com.hr.system.service.ISysDeptService;
import com.hr.system.service.ISysPersonnelClockService;
import com.hr.system.service.ISysUserService;
import com.hr.system.service.IWorkExperienceService;
import com.hr.system.utils.ClockUtils;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author T1734
 */
@Service("indexService")
public class MyIndexService {

    @Autowired
    ISysDeptService sysDeptService;

    @Autowired
    ISysPersonnelClockService clockService;

    @Autowired
    IWorkExperienceService workExperience;

    @Autowired
    ISysUserService userService;


    /**
     * 根据部门编号查询部门信息
     * @return
     */
    public SysDept getDeptById(){
        Long userDeptId = (Long) PermissionUtils.getPrincipalProperty("deptId");
        SysDept sysDept = sysDeptService.selectDeptById(userDeptId);
        return sysDept;
    }

    /**
     * 包名称：com.hr.framework.web.service
     * 类名称：MyIndexService
     * 类描述：获取领导头像
     * 创建人：@author tanglei
     * 创建时间：2021/3/9 16:48
     *
     */
    public String getLeaderAvatar(){
        Long userDeptId = (Long) PermissionUtils.getPrincipalProperty("deptId");
        SysDept sysDept = sysDeptService.selectDeptById(userDeptId);
        String leader = sysDept.getLeader();
        SysUser sysUser = userService.selectUserByUserName(leader);
        return sysUser.getAvatar();
    }

    /**
     * 获取异常次数
     * @return
     */
    public int getErrClockTimes(){
        SysPersonnelClock peronnelClockBean = ClockUtils.getPeronnelClockBean();
        return clockService.getErrClockTimes(peronnelClockBean);
    }

    /**
     * 获取昨天打卡
     * @return
     */
    public SysPersonnelClock getYesterdayClockObj(){
        SysPersonnelClock peronnelClockBean = ClockUtils.getPeronnelClockBean();
        SysPersonnelClock yesterdayPersonnelClock = clockService.getYesterdayPersonnelClock(peronnelClockBean);
        return yesterdayPersonnelClock;
    }

    /**
     * 获取昨天日期
     * @return
     */
    public String getYesterdayDate(){
        return DateUtils.getYesterdayDateStr();
    }

    /**
     * 获取昨天首次打卡时间
     * @return
     */
    public String getYesterdayClockFistClockTime(){
        SysPersonnelClock peronnelClockBean = ClockUtils.getPeronnelClockBean();
        SysPersonnelClock yesterdayPersonnelClock = clockService.getYesterdayPersonnelClock(peronnelClockBean);
        String firstClockTime = ClockUtils.getClockTimeStr(yesterdayPersonnelClock, "first");
        return firstClockTime;
    }

    /**
     * 获取昨天末次打卡日期
     * @return
     */
    public String getYesterdayClockLastClockTime(){
        SysPersonnelClock peronnelClockBean = ClockUtils.getPeronnelClockBean();
        SysPersonnelClock yesterdayPersonnelClock = clockService.getYesterdayPersonnelClock(peronnelClockBean);
        String lastClockTime = ClockUtils.getClockTimeStr(yesterdayPersonnelClock, "last");
        return lastClockTime;
    }



    /**
     * 获取昨天出勤时间
     * @return 出勤时间
     */
    public String getYesterdayWorkTimes(){
        SysPersonnelClock peronnelClockBean = ClockUtils.getPeronnelClockBean();
        SysPersonnelClock yesterdayPersonnelClock = clockService.getYesterdayPersonnelClock(peronnelClockBean);
        String YesterdayWorkTimes = "";
        if (yesterdayPersonnelClock != null) {
            try {
                YesterdayWorkTimes = ClockUtils.getClockTimes(yesterdayPersonnelClock);
            }catch (Exception e){
                e.printStackTrace();
            }
        }else {
            YesterdayWorkTimes = "0.00";
        }
        return YesterdayWorkTimes;
    }

    /**
     * 根据用户获取任职经历
     * @return WorkExperience
     */
    public WorkExperience getWorkExperience() {
        SysUser sysUser = ShiroUtils.getSysUser();
        return workExperience.selectWorkExperienceByUserTenantId(sysUser);
    }

    /**
     * 任职时间
     * @return 时间
     */
    public int getWorkExperienceDate(){
        WorkExperience workExperience = getWorkExperience();
        return DateUtils.getMonthDiff(workExperience.getStartTime(), DateUtils.getDate());
    }

    /**
     * 获取年假天数
     * @return Integer
     */
    public Integer getAnnualLeave(){
        SysUser sysUser = ShiroUtils.getSysUser();
        return sysUser.getAnnualLeave();
    }


    /**
     * 包名称：com.hr.framework.web.service
     * 类名称：MyIndexService
     * 类描述：获取系统当前登录用户
     * 创建人：tanglei
     * 创建时间：2021/3/9 16:32
     *
     */

    public SysUser getSysUser(){
        return ShiroUtils.getSysUser();
    }



}
